package com.blueearth.xingepush;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushConfig;
import com.tencent.android.tpush.XGPushManager;

/**
 * 整合推送服务的代理类
 *
 * @author xlf
 */
public class PushHubProxy {
  private Context context;

  private PushHubListener pushHubListener;

  public PushHubProxy(Context context) {
    this.context = context;
  }

  public void startRegisterPush(String userId) {

    loadConfigFromAssets();

    //开启华为推送调试
    XGPushConfig.setHuaweiDebug(true);

    //打开第三方推送
    XGPushConfig.enableOtherPush(context, true);

    //开启信鸽的日志输出，线上版本不建议调用
    XGPushConfig.enableDebug(context, true);

    XGPushManager.registerPush(context,userId,
      new XGIOperateCallback() {
        @Override
        public void onSuccess(Object data, int flag) {
          if (pushHubListener != null) {
            String token = (String) data;
            pushHubListener.onRegisterSuccess(token, flag);
          }
        }

        @Override
        public void onFail(Object data, int errCode, String msg) {
          if (pushHubListener != null) {
            pushHubListener.onRegisterFail(msg, errCode);//

          }
        }
      });


    // 开始获取token
    XGPushConfig.getToken(context);

  }


  public void setPushHubListener(PushHubListener pushHubListener) {
    this.pushHubListener = pushHubListener;
  }

  public interface PushHubListener {
    public void onRegisterSuccess(String token, int flag);

    public void onRegisterFail(String msg, int errCode);
  }


  private boolean loadConfigFromAssets() {
    Log.i("xlftest", "loadConfigFromAssets:::");
//        InputStream is = null;
//            is = context.getAssets().open("push.properties");
//            Properties properties = new Properties();
//            properties.load(is);

    ApplicationInfo activityInfo = null;
    try {
      activityInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
      e.printStackTrace();
    }


    if (activityInfo != null) {

      String xiaomiAppId = activityInfo.metaData.getString("xiaomiAppId").replace("mi", "");
      String xiaomiAppKey = activityInfo.metaData.getString("xiaomiAppKey").replace("mi", "");
      String meizuAppId = activityInfo.metaData.getString("meizuAppId").replace("mz","");
      String meizuAppKey = activityInfo.metaData.getString("meizuAppKey");

      Log.i("xlftest", "push ids:::" + xiaomiAppId + "/" + xiaomiAppKey + "/" + meizuAppId + "/" + meizuAppKey);
      //小米推送注册的appid和appkey
      XGPushConfig.setMiPushAppId(context, xiaomiAppId);
      XGPushConfig.setMiPushAppKey(context, xiaomiAppKey);
      //注册魅族推送appid和appkey
      XGPushConfig.setMzPushAppId(context, meizuAppId);
      XGPushConfig.setMzPushAppKey(context, meizuAppKey);

    } else {
      Log.i("xlftest", "aactivityInfo null!!");
    }

//            Log.i("xlftest",properties.getProperty("xiaomiAppId")+"/"+properties.getProperty("xiaomiAppKey"));


    return true;
  }


}
